﻿using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using static DataCapture.Helper.PublicHelp;

namespace DataCapture.Helper
{
    public class DataProcessor
    {
        /// <summary>
        /// 插入设备告警数据
        /// </summary>
        /// <param name="alarmData"></param>
        /// <returns></returns>
        public static bool InsertAlarmData(PublicHelp.AlarmData alarmData)
        {
            string sql = @"
                INSERT INTO data_collection_alarm (device_id, fault_Id,status, created_time)
                VALUES (@deviceID, @faultID,@status, @createdTime)";
            try
            {
                int result = DapperHelper.Instance.Connection.Execute(sql, alarmData);
                return result > 0 ? true : false;
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error($"InsertAlarmData 执行sql失败： {ex.Message}");
            }
            return false;
        }

        /// <summary>
        /// 任务台账信息读取
        /// </summary>
        public static void ReadTaskInfoData()
        {

            try
            {
                //string Date = DateTime.Now.ToString("yyyyMMdd");
                string sql = "SELECT * FROM ht_productiveinfo WHERE DATE(CurrentTime) = CURDATE() ";
                DataSet dataSet = null;
                DataTable dt = null;
                List<string> mysqList = ConfigurationHelper.GetAppSettings.ConnectionStrings.MysqlConnstr;//配置表
                if (mysqList.Count > 0)
                {
                    for (int i = 0; i < mysqList.Count; i++)
                    {
                        //连接数据库，读取数据表数据
                        using (dataSet = MySQLConnectHalper.GetDataSet(sql, mysqList[i]))
                        {
                            if (dataSet != null && dataSet.Tables.Count > 0)
                            {
                                dt = dataSet.Tables[0];
                                //转发当前任务信息
                                DeviceDataSave.PlcPopulateData.RWInfo_SaveData(dt, i);

                                //转发当天所有任务信息
                                DeviceDataSave.PlcPopulateData.RWListInfo_SaveData(dt, i);
                            }
                            else
                            {
                                Log4NetHelper.Error($"在数据库中未找到数据。Count:{dataSet.Tables.Count} 线:{i}", true);
                            }
                        }
                    }
                }
               
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error($"读取数据库数据出错：错误内容：{ex.Message}",true);
                throw new Exception($"读取数据库数据出错：错误内容：{ex.Message}"); // 抛出异常以便上层捕获
            }


        }

       
    }
}